<?php

//访问权限

class Access extends Admin_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('access_model');
	}
	
	public function index()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;
		
		$per_page = 20;

		$count = $this->access_model->get_count();
		$access_list = $this->access_model->get_page($p, $per_page, array(), '*', 'id asc');
		
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('access/index'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();

		$this->template->assign('list', $access_list);
		$this->template->assign('page', $this->page);
		$this->template->display('access/list');
	}
	
	//搜索
	public function search()
	{
		$p = intval(get('p'));
		$p = $p ? $p : 1;

		$name = trim(get('name'));
		$directory = trim(get('directory'));
		$controller = trim(get('controller'));
		$method = intval(get('method'));
		$banned = intval(get('banned')) > 0 ? 1 : 0;
		
		$per_page = 20;

		$this->db->select('count(*) as count')->from('access');

		if ($name)
		{
			$this->db->where('name', $name);
		}

		if ($directory)
		{
			$this->db->where('directory', $directory);
		}

		if ($controller)
		{
			$this->db->where('controller', $controller);
		}

		if ($method)
		{
			$this->db->where('method', $method);
		}

		$this->db->where('banned', $banned);

		$result = $this->db->get()->row_array();
		$count = $result['count'];

		$this->db->select('id, name, directory, controller, method, banned')->from('access');

		if ($name)
		{
			$this->db->where('name', $name);
		}

		if ($directory)
		{
			$this->db->where('directory', $directory);
		}

		if ($controller)
		{
			$this->db->where('controller', $controller);
		}

		if ($method)
		{
			$this->db->where('method', $method);
		}

		$this->db->where('banned', $banned);

		$access_list = $this->db->limit($per_page, ($p-1)*$per_page)->get()->result_array();

		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('access/search'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->config($config)->init();

		$this->template->assign('list', $access_list);
		$this->template->assign('page', $this->page);
		$this->template->display('access/list');
	}

	//添加页面
	public function _insert_get()
	{
		$this->template->display('access/insert');
	}
	
	//添加提交
	public function _insert_post()
	{
		$name = trim(post('name')); //权限名称
		$directory = trim(post('directory')); //目录
		$controller = trim(post('controller')); //控制器
		$method = trim(post('method')); //方法
		$banned = intval(post('banned')) > 0 ? 1 : 0; //禁用状态 1是 0否
		
		if ( ! $name)
		{
			msg('名称必须填写！', -1);
		}
		elseif ( ! $controller)
		{
			msg('控制器必须填写！', -1);
		}
		elseif ( ! $method)
		{
			msg('方法必须填写！', -1);
		}
		
		$data = array(
			'name' => $name,
			'directory' => $directory,
			'controller' => $controller,
			'method' => $method,
			'banned' => $banned
		);
		
		if ( ! ($id = $this->access_model->insert($data)))
		{
			log_message('error', '添加访问控制权限失败！原因：写入access表失败！');
			msg('添加访问控制失败！');
		}
		
		$msg = '添加成功！<br /><br /><a href="'.admin_url('access/update', array('id' => $id)).'" class="button">返回修改</a>&nbsp;&nbsp;&nbsp;<a href="'.admin_url('access/insert').'" class="button">继续添加</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//更新页面
	public function _update_get()
	{
		$id = intval(get('id'));
		if ($id < 1)
		{
			msg('非法访问！');
		}
		
		$data = $this->access_model->get($id);
		if ( ! $data)
		{
			msg('访问权限不存在！', admin_url('access/index'));
		}
		
		$this->template->assign('data', $data);
		$this->template->display('access/update');
	}
	
	//更新提交
	public function _update_post()
	{
		$id = intval(post('id')); //权限id
		$name = trim(post('name')); //权限名称
		$directory = trim(post('directory')); //目录
		$controller = trim(post('controller')); //控制器
		$method = trim(post('method')); //方法
		$banned = intval(post('banned')) ? 1 : 0; //禁用状态 1是 0否
		
		if ($id < 1)
		{
			msg('非法访问！');
		}
		elseif ( ! $name)
		{
			msg('名称必须填写！', -1);
		}
		elseif ( ! $controller)
		{
			msg('控制器必须填写！', -1);
		}
		elseif ( ! $method)
		{
			msg('方法必须填写！', -1);
		}
		
		$data = $this->access_model->get($id);
		if ( ! $data)
		{
			msg('访问权限不存在！', admin_url('access/index'));
		}
		
		if ($data['name'] !== $name && $this->access_model->exists(array('name' => $name)))
		{
			msg('名称已经存在！', -1);
		}
		
		$data = array(
			'name' => $name,
			'directory' => $directory,
			'controller' => $controller,
			'method' => $method,
			'banned' => $banned
		);
		
		if ( FALSE === $this->access_model->update($data, $id))
		{
			msg('修改失败！');
		}
		
		$msg = '修改成功！<br /><br /><a href="'.admin_url('access/update', array('id' => $id)).'" class="button">继续修改</a>&nbsp;&nbsp;&nbsp;<a href="'.$this->session->get('back').'" class="button">返回列表</a>';
		msg($msg);
	}
	
	//删除
	public function _delete_ajax()
	{
		$id = intval(get('id'));
		
		if ($id < 1)
		{
			ajax(array('code' => 1, 'msg' => '非法访问！'));
		}
		
		$result = $this->access_model->delete($id);
		
		if ($result !== FALSE)
		{
			ajax(array('code' => 0, 'msg' => '删除成功！'));
		}
		else
		{
			log_message('error', '删除访问权限失败！原因：更新access表失败！');
			ajax(array('code' => 2, 'msg' => '删除失败！'));
		}
	}
	
	//禁用状态改变
	public function banned()
	{
		$this->_banned_ajax();
	}
	
	//禁用状态改变
	protected function _banned_ajax()
	{
		$id = intval(get('id'));
		$banned = intval(get('banned')) > 0 ? 1 : 0;
		
		if ($id < 1)
		{
			ajax(array('code' => 1, 'msg' => '非法访问！'));
		}
		
		$result = $this->access_model->update(array('banned' => $banned), $id);
		
		if (FALSE !== $result)
		{
			ajax(array('code' => 0, 'msg' => '更新成功！'));
		}
		else
		{
			log_message('error', '更改访问权限状态失败！原因：更新access表失败！');
			ajax(array('code' => 2, 'msg' => '更新失败！'));
		}
	}
	
}